package com.votesys.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.votesys.model.Votecontent;
import com.votesys.model.Votetopic;
import com.votesys.service.IContentService;
import com.votesys.util.IUtils;

public class ContentAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IContentService IContentService;
	private IUtils IUtils;
	private int contentid;
	private String contentname;
	private int topicid;
	private String contentabstract;
	private String contentdetail;
	private File pic;
	private String picFileName;
	private InputStream inputStream;
	
	public IContentService getIContentService() {
		return IContentService;
	}

	public void setIContentService(IContentService iContentService) {
		IContentService = iContentService;
	}

	public int getContentid() {
		return contentid;
	}

	public void setContentid(int contentid) {
		this.contentid = contentid;
	}

	public String getContentname() {
		return contentname;
	}

	public void setContentname(String contentname) {
		this.contentname = contentname;
	}

	public int getTopicid() {
		return topicid;
	}

	public void setTopicid(int topicid) {
		this.topicid = topicid;
	}

	public String getContentabstract() {
		return contentabstract;
	}

	public void setContentabstract(String contentabstract) {
		this.contentabstract = contentabstract;
	}

	public String getContentdetail() {
		return contentdetail;
	}

	public void setContentdetail(String contentdetail) {
		this.contentdetail = contentdetail;
	}

	public File getPic() {
		return pic;
	}

	public void setPic(File pic) {
		this.pic = pic;
	}

	public String getPicFileName() {
		return picFileName;
	}

	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public IUtils getIUtils() {
		return IUtils;
	}

	public void setIUtils(IUtils iUtils) {
		IUtils = iUtils;
	}

	public String ShowContent()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		Votecontent VoteContent = IContentService.getContent(contentid);
		request.setAttribute("VoteContent", VoteContent);
		request.setAttribute("returnAction", "UpdateContent");
		return "success";
	}
	
	public String NewContent()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		Votecontent VoteContent = new Votecontent();
		VoteContent.setTopicId(topicid);
		request.setAttribute("VoteContent", VoteContent);
		request.setAttribute("returnAction", "AddContent");
		return "success";
	}
	public String AddContent()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		byte []temppic = IUtils.getFileInputStream(pic);
		if(temppic!=null && contentname!=null && contentabstract!=null && contentdetail!=null){
			IContentService.addContent(topicid, contentname, contentabstract, contentdetail,temppic);
		}else{
			System.out.println("pic is empty.");
		}
		request.setAttribute("topicId", topicid);
		return "ReturnTopic";
	}
	
	public String UpdateContent()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		if(contentid!=0 && contentname!=null && contentabstract!=null && contentdetail!=null){
			Votecontent tempVoteContent = IContentService.getContent(contentid);
			tempVoteContent.setContentName(contentname);
			tempVoteContent.setContentAbstract(contentabstract);
			tempVoteContent.setContentDetail(contentdetail);
			IContentService.updateContent(tempVoteContent);
		}else{
			System.out.println("attr empty.");
		}
		return "ReturnTopic";
	}
	
	public String execute()throws Exception{
		HttpServletRequest request = IUtils.getRequest();
		return "success";
	}
	
	public String getPicOutputStream()throws Exception{
		Votecontent tempVoteContent = IContentService.getContent(contentid);
		if(tempVoteContent!=null){
			byte[] bytes = tempVoteContent.getPic();
			InputStream tempinputStream = new ByteArrayInputStream(bytes);
			this.setInputStream(tempinputStream);
			return "success";
		}else{
			return "error";
		}
	}

}
